<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

// +----------------------------------------------------------------------+
// | Treacle PHP Framework Version 0.10                                   |
// +----------------------------------------------------------------------+
// | Copyright (c) 2007 Treacle.cn All rights reserved.					  |
// +----------------------------------------------------------------------+
// | Licensed under the Apache License, Version 2.0 (the 'License');      |
// | you may not use this file except in compliance with the License.     |
// | You may obtain a copy of the License at                              |
// | http://www.apache.org/licenses/LICENSE-2.0                           |
// | Unless required by applicable law or agreed to in writing, software  |
// | distributed under the License is distributed on an 'AS IS' BASIS,    |
// | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or      |
// | implied. See the License for the specific language governing         |
// | permissions and limitations under the License.                       |
// +----------------------------------------------------------------------+
// | Author: BoPo <ibopo@126.com>										  |
// +----------------------------------------------------------------------+
/**
 * Treacle Benchmark Class file
 *
 * @author BoPo <ibopo@126.com>
 * @link http://www.treacle.cn/
 * @copyright Copyright &copy; 2007 Treacle
 * @license http://www.treacle.cn/license/
 * @version $Id $
 * @package libraries
 */
/**
 * 运行效率计算
 *
 * @package libraries
 * @author BoPo <ibopo@126.com>
 * @since 0.10
 * @version	$Id $
 */
class TBenchmark 
{
	/**
	 * 标签数组
	 * @var array
	 */
	private $marker = array();
	/**
	 * 设置标签
	 *
	 * @access	public
	 * @param	string	$name	name of the marker
	 * @return	void
	 */
	public function mark($name)
	{
		$this->marker[$name] = microtime();
	}
	/**
	 * 计算两个标签之间的运算速度
	 *
	 * @access	public
	 * @param	string	a particular marked point
	 * @param	string	a particular marked point
	 * @param	integer	the number of decimal places
	 * @return	mixed
	 */
	public function elapsed_time($point1 = '', $point2 = '', $decimals = 4)
	{
		if ( ! isset($this->marker[$point1]))
		{
			return '';
		}		
		if ( ! isset($this->marker[$point2]))
		{
			$this->marker[$point2] = microtime();
		}			
		list($sm, $ss) = explode(' ', $this->marker[$point1]);
		list($em, $es) = explode(' ', $this->marker[$point2]);
		return number_format(($em + $es) - ($sm + $ss), $decimals);
	}

	/**
	 * 内存使用量
	 *
	 * @access	public
	 * @return	string
	 */
	public function memory_usage()
	{
		return (!function_exists('memory_get_usage'))?'0':round(memory_get_usage()/1024, 2).'KB';
	}
}
/*
+---------------------------------------------------------------------------
|   Last Modified By BoPo
|   Date: Wed Nov 07 23:54:55 CST 2007
+--------------------------------------------------------------------------
*/
?>